
<div class="moduleTopNav clearfix">
	<div class="fl-l">
		<?php $module->printAddLink($lang->get('add-page')); ?>
	</div>
	<div class="fl-r">
		<?php
		
		$published = (isset($_REQUEST['published'])) ? $_REQUEST['published'] : true;
		echo '<strong>'.$lang->get('published').'</strong> ';
		$module->printBoolDropdown('published', $published, 'window.location=\''.$cosa->system('base-url').'/?m='.$m.'&s='.$s.'&published=\'+this.value');
		
		?>
	</div>
</div>
<table class="datatable">
	<thead>
		<tr>
			<td>&nbsp;</td><?php $extra = '&published='.$published; ?>
			<td><?php $module->printSortLink($lang->get('name'), 'name', $extra); ?></td>
			<td><?php $module->printSortLink($lang->get('title'), 'title', $extra); ?></td>
			<td><?php $module->printSortLink($lang->get('text'), 'text', $extra); ?></td>
			<td><?php $module->printSortLink($lang->get('author'), 'created_by', $extra); ?></td>
			<td><?php $module->printSortLink($lang->get('time-created'), 'time_created', $extra); ?></td>
			<td>&nbsp;</td>
		</tr>
	</thead>
	<tbody>
		<?php
		
		$structure = array('name', 'title', 'text', 'author', 'time_created', 'versionInfo');
		$module->setStructure($structure);
		
		if($published) {
			$sql = "SELECT * FROM ".$db->module('pages')." WHERE deleted = '0' AND published = '1'";
		} else {
			$sql = "SELECT * FROM ".$db->module('pages')." p
					WHERE deleted = '0' AND published = '0'
					AND (
							(
								id > (SELECT id FROM ".$db->module('pages')." WHERE original_id = p.original_id ORDER BY id DESC LIMIT 1, 1)
							)
							OR
							(
								NOT EXISTS (SELECT id FROM ".$db->module('pages')." WHERE original_id = p.original_id ORDER BY id DESC LIMIT 1, 1)
								AND
								id = original_id
							)
						)
					";
			
		}
		
		$res = $module->queryData($sql);
		if(mysql_num_rows($res)) {
			while($row = mysql_fetch_array($res)) {
				
				$publishLink = (!$row['published'] && $module->userCanPublish()) ? '<a href="'.$cosa->system('base-url').'/?m='.$m.'&s='.$s.'&publish='.stripslashes($row['id']).'" title="'.$lang->get('publish').'"><img src="'.$cosa->system('base-url').'/kernel/files/img/icons/s/play.gif" alt="'.$lang->get('publish').'" /></a>' : '';
				$hasNewer = $module->hasUnpublished($row['id']);
				
				$iconHistory = ($row['original_id']) ? '<a href="'.$cosa->system('base-url').'/?m='.$m.'&s=history&oid='.stripslashes($row['original_id']).'" title="'.$lang->get('page-history').'"><img src="'.$cosa->system('base-url').'/kernel/files/img/icons/m/pages.gif" alt="'.$lang->get('page-history').'" align="top" /></a>' : '';
				$versionInfo = ($hasNewer) ? '<a href="'.$cosa->system('base-url').'/?m='.$m.'&s='.$s.'&edit='.$hasNewer.'" title="'.$lang->get('has-newer').'" class="alert">'.$lang->get('has-newer').'</a>' : '';
				
				$data = array(	'id'				=> stripslashes($row['id'])
								,'icon'				=> $iconHistory.'&nbsp '.$publishLink
								,'name'				=> stripslashes($row['name'])
								,'title'			=> stripslashes($row['title'])
								,'text'				=> substr(strip_tags(stripslashes($row['text'])), 0, 70).'...'
								,'author'			=> $user->authorLink($row['created_by'])
								,'time_created'		=> $cosa->datetime($row['time_created'])
								,'versionInfo'		=> $versionInfo
								);
				$module->printTableRow($data);
				
			}
		} else {
			$module->printEmptyTableRow($lang->get('no-items-found'));
		}
		
		?>
	</tbody>
</table>
